package cn.itcast.weatherapp123;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.List;

import cn.itcast.weatherapp123.adapter.FutureWeatherAdapter;
import cn.itcast.weatherapp123.bean.DayWeatherBean;
import cn.itcast.weatherapp123.bean.WeatherBean;
import cn.itcast.weatherapp123.util.NetUtil;

public class MainActivity extends AppCompatActivity {

    private AppCompatSpinner mSpinner;
    private ArrayAdapter<String> mSpAdapter;
    private String[] mCities;

    private TextView tvWeather, tvTem, tvTemLowHigh, tvWin, tvAir;
    private ImageView ivWeather;
    private RecyclerView rlvFutureWeather;
    private FutureWeatherAdapter futureWeatherAdapter;
    private DayWeatherBean dayWeatherBean;

    Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {

                String weather = (String) msg.obj;
                Log.e("fan", "------主线程收到了天气数据-weather-------" + weather);
                Gson gson = new Gson();
                WeatherBean weatherBean = gson.fromJson(weather, WeatherBean.class);
                Log.e("fan", "--解析后的数据-weather----" + weatherBean.toString());

                updateUiOfWeather(weatherBean);

            }
        }
    };

    private void updateUiOfWeather(WeatherBean weatherBean) {

        //判断是否为空，为空则返回
        if (weatherBean == null) {
            return;
        }

        // List<DayWeatherBean>他是一个数组
        List<DayWeatherBean> dayWeathers = weatherBean.getDayWeathers();
        //然后通过每一行数组得到每一行的数据
        dayWeatherBean = dayWeathers.get(0);

        //判断是否为空，为空则返回
        if (dayWeatherBean == null) {
            return;
        }

        //用变量去接收数据

        tvTem.setText(dayWeatherBean.getTem());
        tvWeather.setText(dayWeatherBean.getWea() + "(" + dayWeatherBean.getDate() + dayWeatherBean.getWeek() + ")");
        tvTemLowHigh.setText(dayWeatherBean.getTem2() + "-" + dayWeatherBean.getTem1());
        tvWin.setText(dayWeatherBean.getWin()[0] + dayWeatherBean.getWinSpeed());
        tvAir.setText("空气：" + dayWeatherBean.getAir() + dayWeatherBean.getAirLevel() + "\n" + dayWeatherBean.getAirTips());
        ivWeather.setImageResource(getImResOfWeather(dayWeatherBean.getWeaImg()));

        dayWeathers.remove(0);//去掉当天的天气

        //未来的天气展示
       futureWeatherAdapter=new FutureWeatherAdapter(this,dayWeathers);
       rlvFutureWeather.setAdapter(futureWeatherAdapter);
       LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false);
       rlvFutureWeather.setLayoutManager(linearLayoutManager);

       tvAir.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

               Intent intent=new Intent(MainActivity.this,TipsActivity.class);
               //将数据传递给TipsActivity
               intent.putExtra("tips",dayWeatherBean);

               startActivity(intent);
           }
       });
    }

    //判断天气返回照片
    private int getImResOfWeather(String weaStr) {

        int result = 0;

        switch (weaStr) {
            case "qing":
                result = R.drawable.duoyun;
                break;
            case "yin":
                result = R.drawable.yintian;
                break;
            case "yu":
                result = R.drawable.yun;
                break;
            case "bingbao":
                result = R.drawable.bingbao;
                break;
            case "wu":
                result = R.drawable.wu;
                break;
            case "shachen":
                result = R.drawable.shachen;
                break;
            case "lei":
                result = R.drawable.lei;
                break;
            case "xue":
                result = R.drawable.xue;
                break;


        }
        return result;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //去除标题导航栏
        getSupportActionBar().hide();
        //去除时间和电量等
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }


    private void initView() {

        mSpinner=findViewById(R.id.sp_city);
        mCities=getResources().getStringArray(R.array.cities);
        mSpAdapter=new ArrayAdapter<String>(this,R.layout.sp_item_layout,mCities);
        mSpinner.setAdapter(mSpAdapter);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {

             String selectedCity=mCities[position];

             getWeatherOfCity(selectedCity);

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        tvWeather=findViewById(R.id.tv_weather);
        tvAir=findViewById(R.id.tv_air);
        tvTem=findViewById(R.id.tv_tem);
        tvTemLowHigh=findViewById(R.id.tv_tem_low_high);
        tvWin=findViewById(R.id.tv_win);

        ivWeather=findViewById(R.id.iv_weather);
       rlvFutureWeather=findViewById(R.id.rlv_future_weather);
    }

    private void getWeatherOfCity(final String selectedCity) {

        //开启子线程，请求网络
        new Thread(){
            @Override
            public void run() {

                super.run();
                //请求网络
                String weatherOfCity=NetUtil.getweatherOfCity(selectedCity);
                //使用Handler将数据传递给主线程
                Message message=Message.obtain();//使用Message.obtain()效率会比较高，有利于消息对象的重用
                message.what=0;
                message.obj=weatherOfCity;

                handler.sendMessage(message);

            }
        }.start();
    }
}
